1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static com.google.common.collect.BoundType.CLOSED;
20  import static com.google.common.collect.BoundType.OPEN;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.Multiset.Entry;
24  
25  import java.util.Comparator;
26  import java.util.NoSuchElementException;
27  import java.util.SortedSet;
28  
29  import javax.annotation.Nullable;
30  
31  /**
32   * Provides static utility methods for creating and working with
33   * {@link SortedMultiset} instances.
34   *
35   * @author Louis Wasserman
36   */
37  @GwtCompatible(emulated = true)
38  final class SortedMultisets {
39    private SortedMultisets() {
40    }
41  
42    /**
43     * A skeleton implementation for {@link SortedMultiset#elementSet}.
44     */
45    static class ElementSet<E> extends Multisets.ElementSet<E> implements
46        SortedSet<E> {
47      private final SortedMultiset<E> multiset;
48  
49      ElementSet(SortedMultiset<E> multiset) {
50        this.multiset = multiset;
51      }
52  
53      @Override final SortedMultiset<E> multiset() {
54        return multiset;
55      }
56  
57      @Override public Comparator<? super E> comparator() {
58        return multiset().comparator();
59      }
60  
61      @Override public SortedSet<E> subSet(E fromElement, E toElement) {
62        return multiset().subMultiset(fromElement, CLOSED, toElement, OPEN).elementSet();
63      }
64  
65      @Override public SortedSet<E> headSet(E toElement) {
66        return multiset().headMultiset(toElement, OPEN).elementSet();
67      }
68  
69      @Override public SortedSet<E> tailSet(E fromElement) {
70        return multiset().tailMultiset(fromElement, CLOSED).elementSet();
71      }
72  
73      @Override public E first() {
74        return getElementOrThrow(multiset().firstEntry());
75      }
76  
77      @Override public E last() {
78        return getElementOrThrow(multiset().lastEntry());
79      }
80    }
81  
82    private static <E> E getElementOrThrow(Entry<E> entry) {
83      if (entry == null) {
84        throw new NoSuchElementException();
85      }
86      return entry.getElement();
87    }
88  
89    private static <E> E getElementOrNull(@Nullable Entry<E> entry) {
90      return (entry == null) ? null : entry.getElement();
91    }
92  }
93